//
//  oldHistoryOrdersViewController.m
//  laonianbangSupport
//
//  Created by 勇拓 李 on 18/3/1.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldHistoryOrdersViewController.h"
#import "oldHistoryOrderTableViewCell.h"

@interface oldHistoryOrdersViewController ()
@property (nonatomic, copy)NSArray *dataSource;
@end

static NSString *const identifier = @"oldHistoryOrderTableViewCell";

@implementation oldHistoryOrdersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    [self.tableView registerNib:[UINib nibWithNibName:identifier bundle:nil] forCellReuseIdentifier:identifier];
    self.navigationItem.title = @"评价";
}

#pragma mark - initData
- (void)initData
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}

- (void)refresh
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"pageNum", @"10", @"pageSize", nil];
    
    [self loadData:params ifResetPageNo:YES];
}

- (void)loadMore
{
    NSString *pageNum = [NSString stringWithFormat:@"%ld", (long)pageNo];
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:pageNum, @"pageNum", @"10", @"pageSize", nil];
    [self loadData:params ifResetPageNo:NO];
}

- (void)loadData:(NSDictionary *)params ifResetPageNo:(BOOL)reset
{
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_GetEstimateList params:params success:^(NSDictionary *dic) {
        if (reset) {
            pageNo = 1;
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            pageNo ++;
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        
        weakSelf.dataSource = (id)dic;
        [weakSelf.tableView reloadData];
        
    } error:^(NSString *error) {
        if (reset) {
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        [oldHub warningUser:error];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_dataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self configNormarlCell:tableView indexPath:indexPath];
}

- (UITableViewCell *)configNormarlCell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    oldHistoryOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldHistoryOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    [cell configWithData:_dataSource[indexPath.row]];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 79;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
